package com.fairytask.resource
{
	import com.fairytask.data.Modulator;
	import com.fairytask.event.LoadingEvent;
	import com.hydrotik.queueloader.QueueLoader;
	import com.hydrotik.queueloader.QueueLoaderEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.Security;
	import flash.system.SecurityDomain;
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;
	
	import starling.events.EventDispatcher;
	import starling.textures.Texture;
	import starling.utils.AssetManager;

	/**
	 * 游戏资源中心 
	 * @author game
	 * 
	 */	
	public class ResManager extends EventDispatcher
	{
		private var assetsMrg	:AssetManager;
		private static var _instance	:ResManager;
		
		//数据
		private static var gameTexture	:Dictionary;
		//flash资源数据
		private static var flashData			:Dictionary;
		
		private var model:Modulator;

		public static function get instance():ResManager
		{
			if(null==_instance)
			{
				_instance	=	new ResManager();
			}
			return _instance;
		}
		
		
		
		public function ResManager()
		{
			super();
			assetsMrg	=	new AssetManager();
			gameTexture	=	new Dictionary();
			flashData			=	new Dictionary();
			model	=	Modulator.instance;
		}
		
		public static function addTexture(pPath:String,pComplete:Function=null):void
		{
			//保证实例化
			instance;
			
			instance.assetsMrg.enqueue(pPath);
			instance.assetsMrg.loadQueue(instance.onProgress);
		}
		
		/**
		 * 添加云图 
		 * @param pRes
		 * @param pConfig
		 * 
		 */
		public static function addAtlas(pRes:String,pConfig:String):void
		{
			//保证实例化
			instance;
			
			instance.assetsMrg.enqueue(pConfig,pRes);
			instance.assetsMrg.loadQueue(instance.onProgress);
		}
		
		/**
		 * 取得云图中内容 
		 * @param pAtlas 云图名称
		 * @param pKey 
		 * 
		 */
		public static function getAtlasTexture(pAtlas:String,pKey:String):Texture
		{
			return instance.assetsMrg.getTextureAtlas(pAtlas).getTexture(pKey);
		}
		
		public static function getTexture(pName:String):Texture
		{
			return instance.assetsMrg.getTexture(pName);
		}
		
		/**
		 * 加载进度 
		 */
		private function onProgress(pRatio:Number):void
		{
			if(pRatio==1)
			{
				//gameTexture[]
				model.dispatchEvent(new LoadingEvent(LoadingEvent.RES_READY));
			}
		}
		
		/**
		 * 添加flash资源 
		 * @param pSrc
		 * @param onLoaded
		 * 
		 */
		public static function loadRes(pSrc:String,onLoadComplete:Function=null,pList:Array = null):void
		{
			//保证实例化
			instance;
			//开始加载
			if(isResHave(pSrc))
			{
				if( onLoadComplete!= null)
				{
					onLoadComplete(getResContent(pSrc),pList);
				}				
				return;
			}
			if(!flashData.hasOwnProperty(pSrc))
			{
				//新建1级缓冲
				/*tArray	=	new Array();
				res.HandleProperties("temp"+key,tArray);
				tArray.push([onLoadComplete,pList])*/
				var context:LoaderContext=new LoaderContext();
				if (Security.sandboxType == Security.REMOTE)
				{
					context.securityDomain=SecurityDomain.currentDomain;
				}
				context.applicationDomain=ApplicationDomain.currentDomain;
				
				var queueLoader	:QueueLoader	=	new QueueLoader(false,context);
				queueLoader.addEventListener(QueueLoaderEvent.QUEUE_COMPLETE,onQueueComplete);
				queueLoader.addItem(pSrc);
				queueLoader.execute();
			}
			
			function onQueueComplete(evt:QueueLoaderEvent):void
			{
				flashData[pSrc]=evt.content;
				if(onLoadComplete!=null)
				{
					var t:*	=	getResContent(pSrc)
					onLoadComplete(t,pList);
				}
			}
		}
		
		/**
		 * 取出一个实例 
		 * @param className
		 * @return 
		 * 
		 */
		public static function newResClass(className:String):MovieClip
		{
			if(className == "" || className == null)
			{
				return null;
			}
			else
			{
				var cla		:Object			=	null;				
				var view	:DisplayObject	=	null;
				try
				{
					cla		=	getDefinitionByName(className)
					view	=	new cla() as DisplayObject;
					if(view==null)
					{
						var bmd:BitmapData	=	new cla() as BitmapData;
						view	 = new Bitmap(bmd);
					}
				}
				catch(e:*)
				{
					view	=	null;
				}
				return view as MovieClip;
			}
		}
		
		/**
		 * 取得资源内容 
		 * @param key
		 * @return 
		 * 
		 */
		public static function getResContent(key:String):DisplayObject
		{
			if(!isResHave(key))
			{
				return null;
			}
			
			var tRes:Object	=	flashData[key];
			
			if(tRes == null)
			{
				return null;
			}
			
			if(tRes is Bitmap)
			{
				var myBitmapData	:BitmapData		= 	new BitmapData(tRes.width, tRes.height, true, 0);
				myBitmapData.draw(tRes as Bitmap);
				return new Bitmap(myBitmapData);
			}
			else
			{
				var tClass:Class	=	tRes.constructor;
				
				tRes	=	new tClass();
				
				return  tRes as DisplayObject;
			} 
		}
		
		/**
		 * 判断资源是否缓存
		 * @param key
		 * 
		 */		
		public static function isResHave(key:String):Boolean
		{
			instance;
			
			if(key == null || key == "")
			{
				return false;
			}
			return flashData.hasOwnProperty(key);
		}
	}
}